import Cookies from 'js-cookie'

export function setToken(TOKEN_KEY: string, token: string) {
  Cookies.set(TOKEN_KEY, token, {
    expires: 7, // cookie保存的天数
  })
}
export function getToken(TOKEN_KEY: string) {
  const token = Cookies.get(TOKEN_KEY)
  if (token)
    return token
  else
    return false
}
export function removeToken(TOKEN_KEY: string) {
  Cookies.remove(TOKEN_KEY)
}

/**
 * @description: 在JSON对象和字符串之间转换
 */
export const SStorage = {
  setItem(key: string, val: any) {
    sessionStorage.setItem(key, JSON.stringify(val))
  },
  getItem(key: string) {
    try {
      return JSON.parse(sessionStorage.getItem(key) || '')
    }
    catch (e) {
      return null
    }
  },
  removeItem(key: string) {
    sessionStorage.removeItem(key)
  },
  clear() {
    sessionStorage.clear()
  },
}

export const LStorage = {
  setItem(key: string, val: any) {
    localStorage.setItem(key, JSON.stringify(val))
  },
  getItem(key: string) {
    try {
      return JSON.parse(localStorage.getItem(key) || '')
    }
    catch (e) {
      return null
    }
  },
  removeItem(key: string) {
    localStorage.removeItem(key)
  },
  clear() {
    localStorage.clear()
  },
}
